package com.misday.eatup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class EatUpAdapter extends BaseAdapter {
	
	private static final String TAG = "EatSomethingAdapter";
	
	private Context mContext;
	private ArrayList<HashMap<String, Object>> mDataList;
	private int mResource;
	private LayoutInflater mInflater;
	private ListView mListView;
	private int mEditPosition;

	public EatUpAdapter(Context context, ArrayList<HashMap<String, Object>> list, int resource) {
		super();
		
		mContext = context;
		mDataList = list;
		mResource = resource;
		mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		mEditPosition = -1;
	}

	@Override
	public int getCount() {
		return mDataList.size();
	}

	@Override
	public Object getItem(int position) {
		return mDataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v;
		
		Log.i(TAG, "get View");
		
		mListView = (ListView)parent;
		
		if (convertView == null) {
			v = mInflater.inflate(mResource, parent, false);
			
			ViewHolder holder = new ViewHolder(position, v, parent);
			//holder.del  = (ImageButton)v.findViewById(R.id.something_item_del);
			//holder.edit = (EditText)v.findViewById(R.id.something_item_edit);
			//holder.text = (TextView)v.findViewById(R.id.something_item_eat);
			
			v.setTag(holder);
			
			holder.edit.setTag(holder);
			holder.edit.setOnClickListener(new View.OnClickListener() {			
				@Override
				public void onClick(View v) {
					ViewHolder node = (ViewHolder)v.getTag();
					
					addItem(node.pos);
				}
			});
			
			holder.edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
				@Override
				public void onFocusChange(View edit, boolean hasFocus) {
					ViewHolder holder = (ViewHolder)edit.getTag();
					
					if (hasFocus) {
						addItem(holder.pos);
						mEditPosition = holder.pos;
					} else {
						//fillData(holder.pos, edit);
					}
				}
			});
			
			holder.edit.addTextChangedListener(new TextWatcher() {
				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					Log.i(TAG, "" + mEditPosition + ": afterTextChanged: " + s.toString());
					if (mEditPosition > -1) {
						fillData(mEditPosition, s.toString());
					}
				}
				
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onTextChanged(CharSequence s, int start, int before,
						int count) {
					Log.i(TAG, "" + mEditPosition + ": onTextChanged: " + s.toString());
					//text.setText(s.toString());
				}
			});
			
			
			holder.del.setTag(holder);
			holder.del.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					ViewHolder holder = (ViewHolder)v.getTag();

					holder.edit.setText("");
					clearData(holder.pos);
				}
			});			
		} else {
			v = convertView;
			
			ViewHolder holder = (ViewHolder)v.getTag();
			holder.pos = position; //update the position.
		}
		
		bindView(position, v, parent);

		return v;
	}

	private void bindView(int position, View v, ViewGroup parent) {
		HashMap<String, Object> something = (HashMap<String, Object>)mDataList.get(position);
		String somethingStr = (String)something.get(EatUp.KEY_SOMETHING);
				
		ViewHolder holder = (ViewHolder)v.getTag();

		holder.text.setText(somethingStr);
		holder.edit.setText(somethingStr);
		
		if (position == mEditPosition) {
			holder.edit.setVisibility(View.VISIBLE);
			holder.text.setVisibility(View.GONE);
			
		} else {
			holder.edit.setVisibility(View.GONE);
			holder.text.setVisibility(View.VISIBLE);
		}
	}
	
	private void addItem(int pos) {
		//ListView list = (ListView)v;
		//EatSomethingAdapter adapter = (EatSomethingAdapter)list.getAdapter();
		
		if (!canEatMore(mListView)) {
			Log.i(TAG, "more space for eat");
			return;
		}
		
		HashMap<String, Object> something = new HashMap<String, Object>();
		something.put(EatUp.KEY_SOMETHING, "");
		mDataList.add(something);
		
		this.notifyDataSetChanged();
		//Utility.setListViewHeightBasedOnChildren(list);
		
		//EatUpActivity activity = (EatUpActivity)mContext;
		//activity.sendMessage(EatUp.MSG_UPDATE, pos, 0, null);
	}
	
	/*
	private void fillData(View l) {
		ListView list = (ListView)l;
		List<HashMap<String, Object>> dataList = mDataList;
		
		for (int i = 0; i < list.getCount(); i++) {
			View view = (View)list.getChildAt(i);
			//Log.i(TAG, "" + i + ". view = " + view);
			if (view != null) {
				EditText edit = (EditText)view.findViewById(R.id.something_item_eat);
				HashMap<String, Object> something = (HashMap<String, Object>)dataList.get(i);
				something.remove(EatSomething.KEY_SOMETHING);
				something.put(EatSomething.KEY_SOMETHING, edit.getText().toString());
				Log.i(TAG, "" + i + ". " + edit.getText().toString());
			}
		}
	}
	*/
	
	private void fillData(int position, String data) {
		HashMap<String, Object> something = (HashMap<String, Object>)mDataList.get(position);
		something.put(EatUp.KEY_SOMETHING, data);
		
		mDataList.set(position, something);
	}
	
	private void fillData(int position, View view) {
		EditText edit = (EditText)view;
		
		Log.i(TAG, "" + position + ": " + edit.getText().toString());
		
		fillData(position, edit.getText().toString());
	}
	
	public void fillData(int position, View view, ViewGroup parent) {
		//EditText edit = (EditText)view.findViewById(R.id.something_item_edit);
		//TextView text = (TextView)view.findViewById(R.id.something_item_eat);
		
		//fillData(position, edit.getText().toString());
	}
	
	public void setEditable(int position, View view) {
		ViewHolder holder = (ViewHolder)view.getTag();
		holder.text.setVisibility(View.GONE);
		holder.edit.setVisibility(View.VISIBLE);
		mEditPosition = position;
	}
	
	private void clearData(int position) {
		Log.i(TAG, "pos = " + position);
		
		HashMap<String, Object> something = new HashMap<String, Object>();
		something.put(EatUp.KEY_SOMETHING, "");
		
		mDataList.set(position, something);
	}
	
	private static final int MAX_SPACE = 2;
	private boolean canEatMore(View v) {
		ListView list = (ListView)v;
		
		for (int i = 0, count = 0; i < list.getCount(); i++) {
			/*
			View view = (View)list.getChildAt(i);
			//Log.i(TAG, "" + i + ". view = " + view);			
			if (view == null) {
				return false;
			}
			
			EditText edit = (EditText)view.findViewById(R.id.something_item_eat);
			//Log.i(TAG, "" + i + ". edit = " + edit);
			String something = edit.getText().toString();
			*/
			HashMap<String, Object> somethingMap = (HashMap<String, Object>)mDataList.get(i);
			String something = (String)somethingMap.get(EatUp.KEY_SOMETHING);

			if (TextUtils.isEmpty(something)) {
				count++;					
				if (count >= MAX_SPACE) {
					return false;
				}
			}			
		}
		
		return true;
	}

	private class ViewHolder {
		public int pos;
		public View parent;
		public View item;
		public TextView text;
		public EditText edit;
		public ImageButton del;		
		
		public ViewHolder(int pos, View view, View list) {
			this.pos = pos;
			this.parent = list;
			this.item = view;
		}
		
		public View getViewById(int id) {
			return this.item.findViewById(id);
		}
	}
}
